{% load static %}
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }}</title>
    <link rel="stylesheet" href="{% static 'css/index.css' %}">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .search-container {
    position: relative;
    width: 300px;
}

#search-input {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    font-size: 16px;
}

.suggestions {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    border: 1px solid #ccc;
    border-top: none;
    max-height: 200px;
    overflow-y: auto;
    background-color: #fff;
    list-style: none;
    margin: 0;
    padding: 0;
    z-index: 1000;
}

.suggestions li {
    padding: 10px;
    cursor: pointer;
}

.suggestions li:hover,
.suggestions li.active {
    background-color: #e9e9e9;
}

        .pagination a {
            color: white;
        }
    </style>
</head>
<body>

    <div class="banner">
        <div class="logo">
            <div class="logo-icon">RSS</div>
            <div class="logo-text"><a href="/">博客园</a></div>
        </div>
        <nav class="nav">
            <a href="chat_page/" class="nav-item">AI聊天</a>
            <a href="https://news.cctv.com/" class="nav-item">新闻</a>
            <a href="#" class="nav-item">关注</a>
            <a href="#" class="nav-item">关于</a>
            <a href="#" class="nav-item">更多</a>

        </nav>
        <div class="search-bar" style="position:relative">
            <input type="text" id="search"  class="search-input" placeholder="代码改变世界">
            <ul id="suggestions-list" class="suggestions">

            </ul>

        </div>
        <div class="login" style="color:white;margin-left:20px">
            {% if username %}
            <span>欢迎：<a href="/user_center">{{ username }}</a></span>&nbsp;&nbsp;&nbsp;&nbsp;
            <span><a style="text-decoration: none;color:inherit;" href="/logout">退出</a></span>
            {% else %}
            <span><a style="text-decoration: none;color:inherit;" href="/login">登录</a></span>&nbsp;&nbsp;&nbsp;&nbsp;
            <span><a style="text-decoration: none;color:inherit;" href="/register">注册</a></span>
            {% endif%}

        </div>
    </div>
    <div class="container">
        <div class="main-content">
            {% for article in articles %}
            <div class="article-list">
                <a href="/detail/{{article.id}}" class="article-title">{{ article.title }}</a>
                <div class="article-meta">{{article.publish_time }}</div>
                <div class="article-excerpt">
                   {{ article.description }}
                </div>
            </div>
            {% endfor %}


            <div class="pagination">

            <span>
                {% if articles.has_previous %}
                <a href="/?page=1">首页</a>
                <a href="/?page={{articles.previous_page_number}}">上一页</a>
                {% endif %}

                <a href="/?page={{articles.number}}">{{ articles.number }}</a>
                {% if articles.has_next%}
                <a href="/?page={{articles.next_page_number}}">下一页</a>
                <a href="/?page={{articles.paginator.num_pages}}">尾页</a>
                {% endif %}

            </span>
        </div>

        </div>
        <div class="sidebar">
            <div class="rankings">
                <h2 class="rankings-title">文章阅读排行榜</h2>
                {% for article in top5 %}
                <div class="rank-item">
                    <span class="rank-number"></span>
                    <a href="#" class="rank-title">{{ article.title}}</a>
                </div>
                {% endfor%}

            </div>
        </div>
    </div>
</body>
</html>
<script>
$(document).ready(function() {
    $('#search').on('keydown', function(e) {
        // 获取按下的键的键码
        var key = e.which || e.keyCode;

        // 检查是否按下的是 Enter 键（键码 13）
        if (key === 13) {
            e.preventDefault(); // 阻止默认行为（如果需要）
            // 在这里编写你希望在按下 Enter 时执行的代码
            console.log('Enter 键被按下！');

            // 获取输入框的内容
            let key_word = $("#search").val()
            location.href = "/search?q=" + key_word
        }
    });


    const searchInput = $('#search');
    const suggestionsList = $('#suggestions-list');
    let suggestions;

     function selectSuggestion(index) {
        const selected = suggestions[index];
        searchInput.val(selected.search);
        suggestionsList.empty().hide();


        location.href = "/search?q=" + selected.search
      }


    searchInput.on('input', function() {
        const query = $(this).val().trim();
        let url = "/index/search_suggest?query=" + query
        $.ajax({
            url: url,
            type: "get",
            success: function(response){
                if (response.code == 0){
                    // 数据的渲染

                    suggestions = response.data;
                    suggestions.forEach((item, index) => {
                        console.log(item)
                    const $li = $('<li></li>').text(item.search).attr('data-index', index).on('click', () => selectSuggestion(index));
                    suggestionsList.append($li);
                });
                }
            }
        })

    });

});




</script>